package com.hooker.plugins;

import android.content.Context;
import android.util.Log;

import com.hooker.base.BaseXposedLoader;
import com.hooker.settings.HLog;

import java.lang.reflect.Field;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class PluginAdmob extends BaseXposedLoader {

    public PluginAdmob(Context context, XC_LoadPackage.LoadPackageParam loadParams) {
        super(context, loadParams);
    }

    @Override
    public void hook(Object... calls) {
        
    }

    @Override
    public void hook(Context context, XC_LoadPackage.LoadPackageParam loadParams) {

        try {
            Class patchLoader = loadParams.classLoader.loadClass("java.lang.ClassLoader");

            XposedHelpers.findAndHookMethod(patchLoader, "loadClass", String.class, new XC_MethodHook() {
                @Override
                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                    super.beforeHookedMethod(param);
                }

                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    String name = (String) (param.args[0]);

                    if ("dw".equals(name)) {
                        XposedBridge.hookAllConstructors((Class<?>) param.getResult(), new XC_MethodHook() {
                            @Override
                            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                                if (param.args != null) {
                                    for (Object str : param.args) {
                                        Log.e("TestDemo", "params = " + str);
                                    }
                                }
                                super.beforeHookedMethod(param);
                            }

                            @Override
                            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                                super.afterHookedMethod(param);
                            }
                        });
                    }


                    if ("ct".equals(name)) {
                        Class ctCls = (Class) param.getResult();
                        HLog.o("class load=" + param.thisObject.toString());

                        final Class dCls = ((ClassLoader) param.thisObject).loadClass("com.google.ads.afma.proto2api.d");

                        HLog.o("hook ct.class");
                        if (dCls != null) {
                            HLog.o("load d class=" + dCls.getName());
                        }

                        hookMethod(ctCls, "a", "com.google.ads.afma.proto2api.d", String.class, new XC_MethodHook() {
                            @Override
                            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                                super.beforeHookedMethod(param);
                                HLog.o("hook ct.a before success");
                                Object dObject = param.args[0];
                                Field field = dCls.getField("ad");
                                Object adObject = field.get(dObject);
                                HLog.o("adObject=" + adObject);
                            }

                            @Override
                            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                                super.afterHookedMethod(param);
                                HLog.o("hook ct.a after success");
                                Object dObject = param.args[0];
                                Field field = dCls.getField("ad");
                                Object adObject = field.get(dObject);
                                HLog.o("adObject=" + adObject);
                            }
                        });
                    }

                    super.afterHookedMethod(param);
                }
            });
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
